package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.workcellinfo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.JsmWorkCellInfoMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.WorkCellInfo;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.version.JsmVersionService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.workcellinfo.JsmWorkCellInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by huangxueqian@cqhilink.com on 2016/12/8.
 * 操作单元服务实现
 */

public class JsmWorkCellInfoServiceImpl implements JsmWorkCellInfoService {

    @Autowired
    private JsmWorkCellInfoMapper jsmWorkCellInfoMapper;
    @Autowired
    private JsmVersionService versionService ;


    /**
     * 新增修改-操作单元方法
     *
     * @param workCellInfo
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer addWorkCellInfo(WorkCellInfo workCellInfo, Employee employee) throws Exception {
        Integer resInt = 0;
        if (workCellInfo.getCreatetime() == null) workCellInfo.setCreatetime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
        if (workCellInfo.getCreateopid() == null) workCellInfo.setCreateopid(employee.getEmployeeId());
        workCellInfo.setModifytime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
        workCellInfo.setModifyopid(employee.getEmployeeId());
        if (workCellInfo.getCellId() == null) {
            workCellInfo.setCellId(UniqueUtil.uuid());
            resInt = jsmWorkCellInfoMapper.insertSelective(workCellInfo);
        } else {
            resInt = jsmWorkCellInfoMapper.updateByPrimaryKeySelective(workCellInfo);
        }
        return resInt;
    }

    @Override
    public JSONObject toTable(WorkCellInfo workCellInfo) throws Exception {
        JSONObject jsonObject = new JSONObject();
        /* 进行分页查询分离 */
        jsonObject.put("total", jsmWorkCellInfoMapper.queryCount(workCellInfo));
        List<WorkCellInfo> workCellInfoList = jsmWorkCellInfoMapper.queryTableList(workCellInfo);
        jsonObject.put("rows", JSON.toJSON(workCellInfoList));
        return jsonObject;
    }

    @Override
    public Integer delWorkCellInfo(Map cellId) throws Exception {

        return jsmWorkCellInfoMapper.deleteByPrimaryKeyByCellId(cellId);

    }

    @Override
    public WorkCellInfo queryById(WorkCellInfo workCellInfo) throws Exception {

        return jsmWorkCellInfoMapper.selectByPrimaryKey(workCellInfo.getCellId());
    }


}
